package com.example.competionmanagebackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.competionmanagebackend.entity.Competitions;
import com.example.competionmanagebackend.mapper.CompetitionsMapper;
import com.example.competionmanagebackend.service.CompetitionsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CompetitionsServiceImpl extends ServiceImpl<CompetitionsMapper, Competitions>
        implements CompetitionsService {

    @Resource
    private CompetitionsMapper competitionsMapper;

    @Override
    public Page<Competitions> getCompetitionsPage(int currentPage, int pageSize, Competitions query) {
        Page<Competitions> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Competitions> wrapper = new QueryWrapper<>();
        if (query != null) {
            if (query.getId() != null) {
                wrapper.eq("id", query.getId());
            }
            if (query.getCompetitionName() != null && !query.getCompetitionName().isEmpty()) {
                wrapper.like("competition_name", query.getCompetitionName());
            }
            if (query.getStartDate() != null) {
                wrapper.ge("start_date", query.getStartDate());
            }
            if (query.getEndDate() != null) {
                wrapper.le("end_date", query.getEndDate());
            }
        }
        return this.page(page, wrapper);
    }

    @Override
    public List<Competitions> getAllCompetitions() {
        return competitionsMapper.selectAll();
    }

    @Override
    public void insertCompetition(Competitions competition) {
        competitionsMapper.insertCompetition(competition);
    }

    @Override
    public void updateCompetition(Competitions competition) {
        competitionsMapper.updateCompetition(competition);
    }

    @Override
    public void deleteCompetition(Integer id) {
        competitionsMapper.deleteCompetitionById(id);
    }
}